﻿#include "AuctionInfo.h"

using namespace Server::MirEnvir;

namespace Server::MirDatabase
{

    Server::MirEnvir::Envir *AuctionInfo::getEnvir()
    {
        return Envir::getMain();
    }

    AuctionInfo::AuctionInfo()
    {

    }

    AuctionInfo::AuctionInfo(CharacterInfo *info, UserItem *item, unsigned int price, MarketItemType itemType)
    {
        setEnvir(getEnvir() + 1);
        AuctionID = getEnvir()->NextAuctionID;
        SellerIndex = info->Index;
        SellerInfo = info;
        ConsignmentDate = getEnvir()->getNow();
        Item = item;
        Price = price;
        ItemType = itemType;

        if (itemType == MarketItemType::Auction)
        {
            CurrentBid = Price;
        }
    }

    AuctionInfo::AuctionInfo(BinaryReader *reader, int version, int customversion)
    {
        AuctionID = reader->ReadUInt64();

        Item = new UserItem(reader, version, customversion);
        ConsignmentDate = DateTime::FromBinary(reader->ReadInt64());
        Price = reader->ReadUInt32();
        SellerIndex = reader->ReadInt32();
        Expired = reader->ReadBoolean();
        Sold = reader->ReadBoolean();

        if (version > 79)
        {
            ItemType = static_cast<MarketItemType>(reader->ReadByte());

            CurrentBid = reader->ReadUInt32();

            if (CurrentBid < Price)
            {
                CurrentBid = Price;
            }

            CurrentBuyerIndex = reader->ReadInt32();
        }
    }

    void AuctionInfo::Save(BinaryWriter *writer)
    {
        writer->Write(AuctionID);

        Item->Save(writer);
        writer->Write(ConsignmentDate.ToBinary());
        writer->Write(Price);

        writer->Write(SellerIndex);

        writer->Write(Expired);
        writer->Write(Sold);

        writer->Write(static_cast<unsigned char>(ItemType));
        writer->Write(CurrentBid);
        writer->Write(CurrentBuyerIndex);
    }

    std::string AuctionInfo::GetSellerLabel(bool userMatch)
    {
        switch (ItemType)
        {
            case MarketItemType::GameShop:
                return "";
            case MarketItemType::Consign:
                return userMatch ? (Sold ? "Sold" : (Expired ? "Expired" : "For Sale")) : SellerInfo->Name;
            case MarketItemType::Auction:
                return userMatch ? (Sold ? "Sold" : (Expired ? "Expired" : CurrentBid > Price ? "Bid Met" : "No Bid")) : SellerInfo->Name;
        }

        return "";
    }

    ClientAuction *AuctionInfo::CreateClientAuction(bool userMatch)
    {
        ClientAuction *tempVar = new ClientAuction();
        tempVar->AuctionID = AuctionID;
        tempVar->Item = Item;
        tempVar->Seller = GetSellerLabel(userMatch);
        tempVar->Price = ItemType == MarketItemType::Auction ? CurrentBid : Price;
        tempVar->ConsignmentDate = ConsignmentDate;
        tempVar->ItemType = ItemType;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
